package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.MerchantStore;
import com.rockcent.mall.lib.domain.enumclass.MerchantStoreStatus;
import com.rockcent.mall.lib.dto.MerchantStoreDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * Created by zhangjh on 2016/10/12.
 */
@Repository
public class MerchantStoreRepositoryImpl implements MerchantStoreRepositoryCustom {

    @Autowired
    private ObjectMapper objectMapper;

    private Logger log = LoggerFactory.getLogger(MerchantStoreRepositoryImpl.class);


    @Autowired
    private LocationRepository locationRepository;

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    public  PageResult findByMerchantIdPage(Long merchantId, Long mallId, Whether exchange, String isEnable, String type, Whether isDelete, String name, List<Long> locationIds, PageResult pageResult){
        SQLPageDoing doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from merchant_store ms");
            sql.append(" WHERE ms.is_delete=:isDelete");
            sql.append(" AND ms.merchant_id=:merchantId ");
            sql.append(" AND ms.mall_id=:mallId ");
            sql.append(" AND ms.exchange=:isExchange");
            params.put("isDelete", isDelete.toString());
            params.put("mallId", mallId);
            params.put("merchantId", merchantId);
            params.put("isExchange", Whether.YES.toString());

            if (StringUtils.isNotBlank(isEnable)) {
                sql.append(" AND ms.status=:status");
                params.put("status", isEnable.toString());
            }

            if (StringUtils.isNotBlank(name)) {
                sql.append(" AND (ms.id =:key  OR ms.name  LIKE :keyword)");
                params.put("key", name);
                params.put("keyword", "%" + name + "%");
            }

            if (StringUtils.isNotBlank(type)) {
                sql.append(" AND ms.type=:type");
                params.put("type", type);
            }
            if (locationIds !=null &&!locationIds.isEmpty()) {
                sql.append(" AND ms.location_id in(:locationIds)");
                params.put("locationIds", Arrays.asList(locationIds.toArray()));
            }
            fields.append("ms.*, (select count(*) from merchant_store_customer msc where msc.store_id = ms.id and is_delete = 'NO') verifierCount");

        };

        return doing.go(pageResult, MerchantStoreDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public MerchantStoreDto getMerchantStoreById(Long storeId) {
        SQLObjectDoing<MerchantStoreDto> doing = (sql, fields, params, typeReferenceMap) -> {

            sql.append("select %s from merchant_store ms");
            sql.append(" WHERE  ms.id=:storeId AND ms.is_delete=:isDelete");
            params.put("isDelete",Whether.NO.toString());
            params.put("storeId",storeId);
            fields.append("ms.id,ms.name,ms.number,location_id,ms.type,ms.address,ms.phone,ms.introduction,ms.site");
        };
        return doing.go(MerchantStoreDto.class, jdbcTemplate, log,objectMapper);
    }

    @Override
    public List<MerchantStore> findProductStoreList(Long productId, Long merchantId) {
        SQLListDoing<MerchantStore> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from product_merchant_stores pms left join merchant_store ms on pms.merchant_store_id = ms.id where 1=1 ");
            sql.append(" AND pms.merchant_id = :merchantId");
            sql.append(" AND pms.product_id = :productId");
            sql.append(" AND ms.exchange = :exchange");
            sql.append(" AND ms.is_delete = :isDelete");
            params.put("merchantId", merchantId);
            params.put("productId", productId);
            params.put("exchange", Whether.YES.toString());
            params.put("isDelete", Whether.NO.toString());
            fields.append("ms.id, ms.type, ms.third, ms.location_str ms.number, ms.address, ms.phone");
        };
        return doing.go(MerchantStore.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public List<MerchantStore> findCustomerCouponMerchantStore(Long couponId) {
        SQLListDoing<MerchantStore> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from coupon_merchant_stores cms left join merchant_store ms on cms.store_id = ms.id where 1=1 ");
            sql.append(" AND cms.coupon_id = :couponId");
            sql.append(" AND ms.exchange = :exchange");
            sql.append(" AND ms.is_delete = :isDelete");
            params.put("couponId", couponId);
            params.put("exchange", Whether.YES.toString());
            params.put("isDelete", Whether.NO.toString());
            fields.append("ms.id, ms.name, ms.location_str, ms.address, ms.phone");
        };
        return doing.go(MerchantStore.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public List<MerchantStoreDto> findMerchantStoresList(Long merchantId, Whether exchange, MerchantStoreStatus status, Whether isDelete) {
        SQLListDoing<MerchantStoreDto> doing = (sql, fields, params, typeReferenceMap) -> {
        sql.append("select %s from merchant_store ms where ms.is_delete=:isDelete and ms.merchant_id=:merchantId");
        sql.append(" and ms.exchange=:exchange and ms.status=:status");
            params.put("merchantId",merchantId);
            params.put("status",status.toString());
            params.put("exchange",exchange.toString());
            params.put("isDelete",isDelete.toString());
            fields.append("ms.*");
        };

        return doing.go(MerchantStoreDto.class, jdbcTemplate, log, objectMapper);
    }

}
